package com.chance.cc.crawler.prod.command.job.domain.news.dayao;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.prod.command.job.domain.news.NewsCommonScript.kafkaTopic;
import static com.chance.cc.crawler.prod.command.job.domain.news.NewsCommonScript.metaServiceCommand;

/**
 * created by ljl 20210702
 * 大药网  新闻板块
 */
public class DayaoXinwenCrawlerSchedulerJob {
    public static final String domain = "dayaoMedicinal";
    public static final String site = "news";

    public static final String startUrl = "http://www.dayao.medicinal.cn/xinwen/";

    public static void main(String[] args) {
        publishCrawlerScheduleJobInfo();
        System.out.println(crawlerScheduler().generateCrawlerTriggerKey());
    }

    /**
     * 发布任务信息
     * @return
     */
    public static CrawlerJob publishCrawlerScheduleJobInfo(){

        CrawlerJob crawlerJob = crawlerScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("大药网-新闻-回溯七天");
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return crawlerJob;
    }

    public static CrawlerJob crawlerScheduler(){
        CrawlerRequestRecord articleCrawler = doSearchNewsCrawler();

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-", domain, site, CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-", "crawler", domain, site, "queue")))
                .kafkaResultPipeline(null, kafkaTopic, null)
                .crawlerJobThreadNumber(1)
                .requestRecord(articleCrawler)
                .build();

        //添加评论去重信息

        return crawlerJob;
    }


    public static CrawlerRequestRecord doSearchNewsCrawler(){
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(domain)
                .recordKey(startUrl)
                .httpUrl(startUrl)
                .releaseTime(System.currentTimeMillis())
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*7,null))
                .build();

        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        return requestRecord;
    }

}
